const express = require('express');
const app = express();
const port = 3000;

//设置允许跨域访问该服务.
app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    //Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
    res.header('Access-Control-Allow-Headers', 'Content-Type; application/json; charset=utf-8');
    res.header('Access-Control-Allow-Methods', '*');
    res.header('Content-Type', 'application/json;charset=utf-8');
    next();
});

app.get('/product/list', (req, res) => {
  console.log(req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress ||req.connection.socket.remoteAddress)
    // const response = {
    //     code: 500,
    //     data: null
    // }
    const response = {
        code: 200,
        data: {
            code: 1,
            message: 'success',
            data: {
                name: 'hello world'
            }
        }
    }
    // const response = {
    //     code: 200,
    //     data: {
    //         code: 100422,
    //         message: 'fail',
    //         data: '校验不通过'
    //     }
    // }

    return res.json(response.code, response.data)
});

var server = app.listen(port, '0.0.0.0', () => {
  var host = server.address().address
  var port = server.address().port
  console.log(`${host} Example app listening on port ${port}!`)
});